/*
	Copyright (c) 2007, ANYwebcam.com Pty Ltd. All rights reserved.

	The software in this package is published under the terms of the BSD style 
	license, a copy of which has been included with this distribution in the 
	license.txt file.
*/
package com.anywebcam.di.component
{
	import com.anywebcam.di.*;
	import com.anywebcam.di.error.*;

	/**
	 * Base DIComponent for subclasses that will instantiate an instance
	 */
	public class InstantiatingComponent extends AbstractComponent
	{
		public var parameters:Array;
		
		/**
		 * @inheritDoc
		 * @param parameters An Array of the hints for the parameters to use in the constructor injection
		 */
		public function InstantiatingComponent( key:Object, type:Class, parameters:Array = null )
		{
			super( key, type );
			
			checkConcrete();
			
			this.parameters = parameters;
		}
		
		/**
		 * Resolves a parameter value from from a String, Function, or Class
		 * 
		 * @param container The DIContainer to retrieves instance from if required
		 * @param parameter The Parameter to resolve the value for
		 */
		protected function getParameterValue( container:DIContainer, parameter:Object ):Object
		{
			var value:Object = parameter;

			if( value is String )
			{
				value = container.getInstance( value );
			}
			
			if( value is Function )
			{
				value = (value as Function)( container, this, parameter );
			}
			
			if( value is Class )
			{
				value = container.getInstance( value as Class );
			}
			
			return value;
		}
		
		/**
		 * @throws NotConcreteTypeError
		 */
		protected function checkConcrete():void
		{
			if( !classInfo.isConcrete )
			{
				throw new DIComponentError( this, 'Non concrete type:'+ type );
			}
		}
	}
}